﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

using Dxr.Police.Models;
using Dxr.Police.IACS.Models;
using Dxr.Police.IACS.Services;
using Dxr.Police.IACS.FullText;
using Dxr.Police.Web.Http.Controllers;

using Zongsoft.Data;
using Zongsoft.Common;
using Zongsoft.Web.Http;

namespace Dxr.Police.IACS.Web.Http.Controllers
{
    public class InformationController : ControllerBase<Resource, ResourceConditional, ResourceService>
    {
        public InformationController(Zongsoft.Services.IServiceProvider serviceProvider) : base(serviceProvider)
        {
        }

        [Zongsoft.Services.ServiceDependency]
        public IDataSource DataSource
        { get; set; }

        [Zongsoft.Services.ServiceDependency]
        public ZDSJ_ZYZHService ZDSJ_ZYZHService
        { get; set; }

        public Options.CommonElement Option
        { get; set; }

        public override HttpResult Post(Resource model)
        {
            var result = new HttpResult() { };

            try
            {
                using (var tran = new Zongsoft.Transactions.Transaction())
                {
                    model.CreatedTime = DateTime.Now;
                    model.CreatorId = UserInfo.UserId;
                    model.CreatorName = UserInfo.Name;
                    model.Id = Guid.NewGuid().ToString();
                    model.Count = DataSource.ImportFromExcel(model.Path, model.Type, UserInfo);

                    result = base.Post(model);

                    ZDSJ_ZYZHService.Insert(new ZDSJ_ZYZH()
                    {
                        id = model.Id,
                        bmmc = model.CollectorDepartmentName,
                        sjlx = model.Type.GetEnumEntry().Description,
                        zyL = model.Count,
                        zhgxdate = model.CreatedTime
                    });

                    tran.Commit();
                }
            }
            catch (ImportException e)
            {
                result.msg = e.Message;
                result.ret = HttpResult.Error;
            }
            catch (Exception e)
            {
                result.msg = "导入失败！请联系管理员！";
                result.ret = HttpResult.Error;
                Zongsoft.Diagnostics.Logger.Error(e);
            }

            return result;
        }

        [HttpPost]
        [HttpPaging]
        public HttpResult FullTextSearch([FromBody]FullTextConditional condition, [FromUri]Paging paging)
        {
            var result = new HttpResult()
            {
                ret = HttpResult.Success,
            };

            try
            {
                result.content = DataSource.Select(condition, paging);
            }
            catch (Exception e)
            {
                result.msg = "查询失败！请联系管理员！";
                result.ret = HttpResult.Error;
                Zongsoft.Diagnostics.Logger.Error(e);
            }

            return result;
        }

        protected override object ToDisplayModel(Resource model)
        {
            return AutoMapper.Mapper.Map<Models.ResourceDisplayModel>(model);
        }
    }
}
